package com.guoguo.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.StaticResource;
import com.guoguo.entity.Attention;
import com.guoguo.entity.PointFlow;
import com.guoguo.entity.UserExt;
import com.guoguo.entity.Users;
import com.guoguo.mapper.InviteRelationMapper;
import com.guoguo.mapper.PointFlowMapper;
import com.guoguo.mapper.UserExtMapper;
import com.guoguo.mapper.UsersMapper;
import com.guoguo.service.IUserExtService;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.RedisUtils;
import com.guoguo.utils.UUIDUtils;

/**
 * Created by Administrator on 2017/8/9 0009.
 */
@Service
public class UserExtServiceImpl implements IUserExtService {

    //UserExtr接口
    @Autowired
    private UserExtMapper userExtMapper;
    
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private InviteRelationMapper inviteRelationMapper;
    
    @Autowired
    private PointFlowMapper pointFlowMapper;

    @Override
    public UserExt updateUserExt(UserExt userExt) {
        int num = userExtMapper.updateUserExt(userExt);
        UserExt userExts = userExtMapper.findUserExt(userExt.getUserId());
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        String icon = AliOssUtils.getImageUrl(userExts.getIcon());//, aliOssImageConfig);
        userExts.setIcon(icon);
        return userExts;

    }

    @Override
	public List<UserExt> selectUserExt() {
        return userExtMapper.selectUserExt();
    }

    @Override
    public UserExt findUserExt(String userId) {
        UserExt userExts = userExtMapper.findUserExt(userId);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        String icon = AliOssUtils.getImageUrl(userExts.getIcon());//, aliOssImageConfig);
        userExts.setArgs5(userExts.getIcon());
        userExts.setIcon(icon);
        return userExts;
    }


    @Override
    public void insertUserExt(UserExt userExt) {
        userExtMapper.insertUserExt(userExt);
    }

    @Override
    public int updateUserExtJY(String userId) {
        return userExtMapper.updateUserExtJY(userId);

    }

    @Override
    public int updateUserExtQXJY(String userId) {
        return userExtMapper.updateUserExtQXJY(userId);
    }

    @Override
    public void updateUserMember(String rechargeId, int payType, UserExt userExt) {
        RedisUtils redisUtils = RedisUtils.getInstance();
        String payTypeMap = redisUtils.hget(StaticResource.SYS_INIT_RECHARGE_KEY, rechargeId);
        JSONObject rechargeJson = JSONObject.parseObject(payTypeMap);
        userExtMapper.updateUserExt(userExt);
    }

    @Override
    public Attention findUserAttention(Map<String, String> par) {
        return userExtMapper.getUserAttention(par);
    }

    /**
     * @Description： 更新邀请人的积分,每天前10次增加
     * @author: WYZ
     * @since: 2017年10月25日 下午8:05:33
     */
    @Override
    public Integer addSrcUserPoint(String isScrUser,String phone) {
    	Integer addCount = null;
        Integer addCountFan = null;
        Integer addAllFan = null;
        Integer invitDayCount = null;
    	//查询邀请人的当天增加几次果币
        invitDayCount = pointFlowMapper.selectInvitDayCount(isScrUser,new Date());
    	if(invitDayCount < 10){
    		PointFlow pointFlowInvit = new PointFlow();
            pointFlowInvit.setOrderNo(UUIDUtils.getUUID());
            pointFlowInvit.setPaymentType(3);
            pointFlowInvit.setOrderDetail("邀请好友，发起邀请加果币");
            pointFlowInvit.setCreateTime(new Date());
            pointFlowInvit.setArgs1(isScrUser);
            pointFlowInvit.setMoney("1000");
            inviteRelationMapper.updateStatus(isScrUser);
            addCount = userExtMapper.updateExt(isScrUser,1000);
            pointFlowMapper.insertSelective(pointFlowInvit);
    	}
        PointFlow pointFlow = new PointFlow();
        pointFlow.setOrderNo(UUIDUtils.getUUID());
        pointFlow.setPaymentType(3);
        pointFlow.setOrderDetail("邀请好友，接受邀请加果币");
        pointFlow.setCreateTime(new Date());
        pointFlow.setMoney("1000");
        if(StringUtils.isNotBlank(phone) && !phone.equals("null")){
        	Users users = new Users();
        	users = usersMapper.selectUserByPhone(phone);
        	if(users != null){
        		pointFlow.setArgs1(users.getUserId());
        		addCount = userExtMapper.updateExt(users.getUserId(),1000);
        	}
        }
        addCountFan = pointFlowMapper.insertSelective(pointFlow);
        if(addCountFan > 0 && addCount > 0){
        	addAllFan = 1;
        }
        return addAllFan;
    }

}